#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 305;
const int MX = 300;

int n, p;
ll f[N][N][N], sum1[N][N][N], sum2[N][N][N];
ll ans[N][N];
void add(ll& x, ll y) { x = (x + y) % p; }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t >> p;
  f[0][0][0] = 1;
  rep(i, 0, MX) sum1[0][i][0] = 1;
  rep(i, 0, MX) sum2[0][0][i] = 1;
  rep(i, 1, MX) {
    rep(j, 0, i) {
      rep(k, 0, i) {
        if (j) add(f[i][j][k], sum1[i - 1][j - 1][k]);
        if (k) add(f[i][j][k], sum2[i - 1][j][k - 1]);
        sum1[i][j][k] = f[i][j][k];
        sum2[i][j][k] = f[i][j][k];
      }
    }
    rep(j, 0, i) rep(k, 1, i) {
      add(sum1[i][k][j], sum1[i][k - 1][j]);
      add(sum2[i][j][k], sum2[i][j][k - 1]);
    }
  }
  rep(i, 1, MX) rep(j, 1, MX) {
    ans[i][j] = ans[i][j - 1];
    if (j <= i) {
      rep(k, 0, j - 1) add(ans[i][j], sum1[i - 1][j - 1][k]);
      rep(k, 1, j - 2) add(ans[i][j], sum1[i - 1][k - 1][j]);
      rep(k, 1, j - 2) add(ans[i][j], sum2[i - 1][j][k - 1]);
    }
  }
  while (t--) {
    int n, x;
    cin >> n >> x;
    cout << ans[n][x - 1] << endl;
  }
  return 0;
}